package com.scyshkj.university.wechat.pay;

import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

/**
 * Created by 唐旭 on 2016/11/21.
 */
@Component
public class RechargeFormDtoValidator implements Validator {
    @Override
    public boolean supports(Class<?> aClass) {
        return RechargeFormDto.class.equals(aClass);
    }

    @Override
    public void validate(Object o, Errors errors) {
        RechargeFormDto formDto = (RechargeFormDto) o;
        String amount = formDto.getAmount();
        try {
            Float aFloat = Float.valueOf(amount);
            if (aFloat <= 0) {
                errors.rejectValue("amount", "", "金额必须大于0");
            }
        } catch (NumberFormatException e) {
            errors.rejectValue("amount", "", "金额输入错误");
        }
    }
}
